using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint.Administration;

namespace WMOSSExperts.Diagnostics
{
    [CLSCompliant(false)]
    [System.Runtime.InteropServices.Guid("33DA855C-8B23-4031-9520-0E476AC3403C")]
    public class DiagnosticsManager : SPService, IDiagnosticsManager
    {
        // Collection of WMOSS Experts catgories
        private SortedList<String, DiagnosticLevel> _categories = null;
        public const String SERVICENAME = "WMOSSExperts Diagnostic";
        private Boolean _isDirty;

        #region "Constructor"
        public DiagnosticsManager()
            : base(SERVICENAME, SPFarm.Local)
        {
            _categories = new SortedList<String, DiagnosticLevel>();
            DiagnosticLevel coreLevel = new DiagnosticLevel(LogCategory.WMOSSExpertsCore.ToString());
            _categories.Add(coreLevel.Id, coreLevel);
            coreLevel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(DiagnosticLevel_PropertyChanged);
            _isDirty = false;
        }

        void DiagnosticLevel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            _isDirty = true;
            DiagnosticLevel category = (sender as DiagnosticLevel);
            if (!this.Properties.ContainsKey(category.Id))
                this.Properties.Add(category.Id, category);
            else
                this.Properties[category.Id] = category;
        }
        #endregion


        #region IDiagnosticsManager Members

        public IDiagnosticsLevel GetItem(string name)
        {
            return _categories[name];
        }

        public IEnumerable<IDiagnosticsLevel> GetItems()
        {
            List<IDiagnosticsLevel> items = new List<IDiagnosticsLevel>();

            foreach (DiagnosticLevel category in _categories.Values)
            {
                items.Add(category);
            }

            return items;
        }

        public void ResetAll()
        {
            foreach (DiagnosticLevel category in _categories.Values)
            {
                category.TraceSeverity = TraceSeverity.None;
                category.EventSeverity = EventSeverity.None;
            }
        }

        public void ResetItem(IDiagnosticsLevel item)
        {
            item.TraceSeverity = TraceSeverity.None;
            item.EventSeverity = EventSeverity.None;
        }

        public void SetAll(TraceSeverity traceSeverity)
        {
            foreach (DiagnosticLevel category in _categories.Values)
            {
                category.TraceSeverity = traceSeverity;
            }
        }

        public void SetAll(TraceSeverity traceSeverity, EventSeverity eventSeverity)
        {
            foreach (DiagnosticLevel category in _categories.Values)
            {
                category.TraceSeverity = traceSeverity;
                category.EventSeverity = eventSeverity;
            }
        }

        /// <summary>
        /// Updates diagnostic configuration into the SharePoint config database
        /// </summary>
        //public override void Update()
        //{
        //    // Update each category
        //    foreach (DiagnosticLevel category in _categories.Values)
        //    {
        //        if (!this.Properties.ContainsKey(category.Id))
        //            this.Properties.Add(category.Id, category);
        //        else
        //            this.Properties[category.Id] = category;
        //    }

        //     base.Update();

        //    //}
        //}

        public override void Update()
        {
            //base.Update();
            if (_isDirty | this.IsNew)
            {
                base.Update();
                _isDirty = false;
                this.Invalidate();
            }
        }

        public Boolean IsNew
        {
            get
            {
                return (SPFarm.Local.Services.GetValue<DiagnosticsManager>(SERVICENAME) == null);
            }
        }

        /// <summary>
        /// Reload the configured categories after deserialization
        /// </summary>
        protected override void OnDeserialization()
        {

            base.OnDeserialization();
            
            foreach (DiagnosticLevel category in _categories.Values)
            {
                DiagnosticLevel persitedCategory = this.Properties[category.Id] as DiagnosticLevel;

                if (persitedCategory != null)
                {
                    category.EventSeverity = persitedCategory.EventSeverity;
                    category.TraceSeverity = persitedCategory.TraceSeverity;
                }
            }
            _isDirty = false;


        }

        #endregion
    }
}
